﻿using System;
using System.IO;
using DataAccess.Entities;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace DataAccess
{
    class Program
    {
        private const string DbFile = "firstProgram.db";

        static void Main()
        {
            // create our NHibernate session factory
            var sessionFactory = CreateSessionFactory();

            using (var session = sessionFactory.OpenSession())
            {
                // populate the database
                using (var transaction = session.BeginTransaction())
                {
                    var female = new Gender { Type = "Female" };
                    var male = new Gender {Type = "Male"};

                    var single = new RelationshipStatus { Status = "Single" };
                    var inRelationship = new RelationshipStatus { Status = "In A Relationship" };
                    var complicated = new RelationshipStatus { Status = "It's Complicated" };
                    var engaged = new RelationshipStatus { Status = "Engaged" };
                    var married = new RelationshipStatus { Status = "Married" };

                    string[] lines = File.ReadAllLines("interests.txt");

                    foreach (string line in lines)
                    {
                        var interest = new Interest {Name = line};
                        session.SaveOrUpdate(interest);
                    }


                    session.SaveOrUpdate(female);
                    session.SaveOrUpdate(male);
                    session.SaveOrUpdate(single);
                    session.SaveOrUpdate(inRelationship);
                    session.SaveOrUpdate(complicated);
                    session.SaveOrUpdate(engaged);
                    session.SaveOrUpdate(married);

                    transaction.Commit();
                }
            }

            using (var session = sessionFactory.OpenSession())
            {
                // retreive all stores and display them
                using (session.BeginTransaction())
                {
                    var status = session.CreateCriteria(typeof(RelationshipStatus))
                        .List<RelationshipStatus>();

                    foreach (var s in status)
                    {
                        Console.WriteLine(s.Status);
                    }
                }
            }

            Console.ReadKey();
        }

        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                    .ConnectionString("Data Source=scarlet.arvixe.com;Initial Catalog=zeenckdev;" +
                        "Persist Security Info=True;User ID=zeenck;Password=zeenck2011"))
               .ExposeConfiguration(BuildSchema)
                        .Mappings(m =>
                    m.FluentMappings.AddFromAssemblyOf<Program>())
                .BuildSessionFactory();

//                            .Database(SQLiteConfiguration.Standard
//                    .UsingFile(DbFile))
            // .ExposeConfiguration(BuildSchema)
        }

        private static void BuildSchema(Configuration config)
        {
            // delete the existing db on each run
            if (File.Exists(DbFile))
                File.Delete(DbFile);

            // this NHibernate tool takes a configuration (with mapping info in)
            // and exports a database schema from it
            new SchemaExport(config)
                .Create(false, true);
        }
    }
}
